﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PipeHitter
{

    // Defines the data protocol for reading and writing strings on our stream
    public class PipeStreamUtil
    {
        public void WriteString(Stream pipe, string message)
        {
            
            var mbyes = Encoding.Unicode.GetBytes(char.MinValue +  message + char.MinValue);
            pipe.Write(mbyes, 0, mbyes.Length);

        }

        public string ReadString(Stream pipe)
        {
            var resp = string.Empty;
            var buffer = new byte[10280];
            do
            {
                var read = pipe.Read(buffer, 0, buffer.Length);
                if (read < 1) { return resp; }
                resp += Encoding.Unicode.GetString(buffer, 0, read);
                if (resp.Length < 1 || resp[0] != char.MinValue) { 
                    return resp; }

            } while (resp[resp.Length -1] != char.MinValue);
            return resp.Substring(1, resp.Length - 2);
        }

      
    }
}
